//
//  ZServer.m
//  LuckyTools
//
//  Created by ZhàoYǒng on 16/1/11.
//  Copyright © 2016年 ZhàoYǒng. All rights reserved.
//

#import "ZServer.h"

@interface ZServer ()

@property (nonatomic, copy) NSString *serverBaseURL;
@property (nonatomic, copy) NSString *serverAPIComponent;

@end

static ZServer *serverInstance = nil;

@implementation ZServer

+ (instancetype)server {
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        serverInstance = [[self alloc] init];
    });
    return serverInstance;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _serverBaseURL = @"";
        _serverAPIComponent = @"";
    }
    return self;
}

- (void)bindBaseURL:(NSString *)baseURL apiComponent:(NSString *)apiComponent {
    _serverBaseURL = baseURL;
    _serverAPIComponent = apiComponent;
}

#pragma mark - Get method

- (NSString *)serverURL {
    NSString *serverURL = NSProcessInfo.processInfo.environment[@"SERVER_URL"];
    if (serverURL.length > 0) return serverURL;
    if (self.serverAPIComponent.length == 0) {
        return self.serverBaseURL;
    } else {
        return [NSString stringWithFormat:@"%@/%@", self.serverBaseURL, self.serverAPIComponent];
    }
}

@end
